package com.game.connection.message;

import java.util.StringTokenizer;

public class BulletMessage extends Message {
	
	private boolean alive;
	
	public String write() {
		return "bullet{##&#id:" + getId() + "##&#;##&#velX:" + getVelX()
				+ "##&#;##&#isalive:" + alive 
				+ "##&#;##&#velY:" + getVelY() + "##&#;##&#faceAngle:"
				+ getFaceAngle() + "##&#;##&#x:" + getX() + "##&#;##&#y:"
				+ getY() + "##&#}";
	}
	
	public boolean isAlive() {
		return alive;
	}

	public void setAlive(boolean isalive) {
		this.alive = isalive;
	}

	public BulletMessage() {
		alive = false;
		// TODO Auto-generated constructor stub
	}
	
	public BulletMessage(int id, String txt) {
		this.setTxt(txt);
		this.setId(id);
		load();
		
	}
	
	@Override
	public void load() {
		StringTokenizer token = new StringTokenizer(this.getTxt(),"##&#");
		String value ;
		while(token.hasMoreElements()){
			
			value =(String) token.nextElement();
			if( value.indexOf("id:")>-1){
				value = removeIdentification(value);
				setId(parseStringToInteger(value));
			}

			if( value.indexOf("name:")>-1){
				value = removeIdentification(value.replace("null" , ""));
				setName(value);
			}
			
			if( value.indexOf("velX:")>-1){
				value = removeIdentification(value);
				setVelX(parseStringToDouble(value));
			}
			
			if( value.indexOf("velY:")>-1){
				value = removeIdentification(value);
				setVelY(parseStringToDouble(value));
			}
			
			if( value.indexOf("faceAngle:")>-1){
				value = removeIdentification(value);
				setFaceAngle(parseStringToDouble(value));
			}
			if( value.indexOf("isalive:")>-1){
				value = removeIdentification(value);
				setAlive(parseStringToBoolean(value));
			}
			
			
			if( value.indexOf("x:")>-1){
				value = removeIdentification(value);
				setX(parseStringToDouble(value));
			}
			
			if( value.indexOf("y:")>-1){
				value = removeIdentification(value);
				setY(parseStringToDouble(value));
			}
			if( value.indexOf("msg:")>-1){
				value = removeIdentification(value.replace("null" , ""));
				setMessage(value);
			}
			
			
		}

	}

	
	@Override
	public String toString() {
		return "BulletMessage [ "
				+ " getVelX()=" + getVelX() + ", getVelY()=" + getVelY()
				+ ", getId()=" + getId() + ", getName()=" + getName()
				+ ", getX()=" + getX() + ", getFaceAngle()=" + getFaceAngle()
				+ ", getY()=" + getY() + ", getMessage()=" + getMessage()
				 + "]";
	}

	public static void main(String[] args) {
		BulletMessage bull = new BulletMessage(2,
		"bullet{##&#id:0##&#;##&#name:null##&#;##&#isalive:false##&#;##&#velX:3.6739403974420595E-17##&#;##&#velY:-0.6##&#;##&#faceAngle:0.0##&#;##&#x:320.0##&#;##&#y:226.90000000000006##&#;##&#msg:null##&#}");
		
		
		System.out.println(bull.getVelX());
		System.out.println(bull.write());
	}
	
}
